package com.virtium.vpm.dao.hibernate;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.virtium.vpm.dao.MajorTaskDAO;
import com.virtium.vpm.hibernate.model.MajorTask;


/**
 * A data access object (DAO) providing persistence and search support for
 * MajorTask entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.virtium.vpm.hibernate.model.MajorTask
 * @author MyEclipse Persistence Tools
 */

public class MajorTaskDAOHibernate extends BaseHibernateDAO implements MajorTaskDAO{
	private static final Log log = LogFactory.getLog(MajorTaskDAOHibernate.class);
	// property constants
	public static final String PROJECT_ID = "projectId";
	public static final String MAJOR_TASK = "majorTask";
	public static final String USER_ID = "userId";
	public static final String COST = "cost";
	public static final String ACHIEVED = "achieved";
	public static final String KEY_ISSUE = "keyIssue";
	public static final String DECISION = "decision";

	public void save(MajorTask transientInstance) {
		log.debug("saving MajorTask instance");
		try {
			Transaction tx = getSession().beginTransaction();
			hsession.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}finally {
    		closeSession();
    	}
	}
	
	public void update(MajorTask majorTask){
    	
    	try {
    		if (majorTask != null) {
    			//getSession().update(majorTask);
        		Transaction tx = getSession().beginTransaction();
        		hsession.update(majorTask);        		
        		tx.commit();   			
    		}
     	} catch (RuntimeException e) {
    		log.error("Unable to update user:" + e.getMessage() + " - " +
    				e.getClass().getName());
    		throw e;
    	}finally {
    		closeSession();
    	}
    }
	public void delete(MajorTask persistentInstance) {
		log.debug("deleting MajorTask instance");
		try {
			persistentInstance = findById(persistentInstance.getId());
			Transaction tx = getSession().beginTransaction();			
			hsession.delete(persistentInstance);
			tx.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally {
    		closeSession();
    	}
	}

	public MajorTask findById(java.lang.Integer id) {
		log.debug("getting Type instance with id: " + id);
		try {
			MajorTask instance = (MajorTask) getSession().get(
					"com.virtium.vpm.hibernate.model.MajorTask", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally {
    		closeSession();
    	}
	}

	public List<MajorTask> findByExample(MajorTask instance) {
		log.debug("finding MajorTask instance by example");
		try {
			List<MajorTask> results = getSession().createCriteria(
					"com.virtium.vpm.hibernate.model.MajorTask").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<MajorTask> findByProperty(String propertyName, Object value) {
		log.debug("finding MajorTask instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MajorTask as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<MajorTask> findByProjectId(Object projectId) {
		return findByProperty(PROJECT_ID, projectId);
	}

	public List<MajorTask> findByMajorTask(Object majorTask) {
		return findByProperty(MAJOR_TASK, majorTask);
	}

	public List<MajorTask> findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List<MajorTask> findByCost(Object cost) {
		return findByProperty(COST, cost);
	}

	public List<MajorTask> findByAchieved(Object achieved) {
		return findByProperty(ACHIEVED, achieved);
	}

	public List<MajorTask> findByKeyIssue(Object keyIssue) {
		return findByProperty(KEY_ISSUE, keyIssue);
	}

	public List<MajorTask> findByDecision(Object decision) {
		return findByProperty(DECISION, decision);
	}

	public List<MajorTask> findAll() {
		log.debug("finding all MajorTask instances");
		try {
			String queryString = "from MajorTask";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally {
    		closeSession();
    	}
	}

	public MajorTask merge(MajorTask detachedInstance) {
		log.debug("merging MajorTask instance");
		try {
			MajorTask result = (MajorTask) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MajorTask instance) {
		log.debug("attaching dirty MajorTask instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MajorTask instance) {
		log.debug("attaching clean MajorTask instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}